using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace visAd.ExceptionHandler
{
    public class vaCustomException : ApplicationException
    {
        //private static string _SCOMConnectionString = "Domain=test;UserName=Administrator;password=password";
        //private static string _SCCMConnectionString = "Domain=test;UserName=Administrator;password=password";
        //private static string _SCOMServerName = "10.50.204.245";
        //private static string _SCCMServerName = "10.50.204.36";


        //public static string SCOMConnectionString
        //{
        //    get { return vaCustomException._SCOMConnectionString; }
        //    set { vaCustomException._SCOMConnectionString = value; }
        //}
        //public static string SCCMConnectionString
        //{
        //    get { return vaCustomException._SCCMConnectionString; }
        //    set { vaCustomException._SCCMConnectionString = value; }
        //}
        //public static string SCOMServerName
        //{
        //    get { return vaCustomException._SCOMServerName; }
        //    set { vaCustomException._SCOMServerName = value; }
        //}
        //public static string SCCMServerName
        //{
        //    get { return vaCustomException._SCCMServerName; }
        //    set { vaCustomException._SCCMServerName = value; }
        //}

        private string ErrorMessage = string.Empty;
        private Exception ExceptionObj;
        private vaCallStackTracer CallTrace = new vaCallStackTracer();
        private bool IsBusinessValidation = false;
     
        public vaCustomException(Exception exception, string className,
                                 string methodName, string errorMsg)
            : base(errorMsg.Length == 0 ? exception.Message : errorMsg, exception)
        {

            IsBusinessValidation = false;
            ExceptionObj = exception;
            ErrorMessage = errorMsg + "." + ExceptionObj.Message;
            CallTrace.Add(className, methodName);

        }

        public vaCustomException(Exception exception, string className, string methodName)
            : base(exception.Message, exception)
        {

            IsBusinessValidation = false;
            ExceptionObj = exception;
            ErrorMessage = ExceptionObj.Message + ", Please contact Admin for further details";
            CallTrace.Add(className, methodName);
        }

        public vaCustomException(string customMessage)
            : base(customMessage)
        {
            IsBusinessValidation = true;
            ErrorMessage = customMessage;
        }

        /// <summary>
        /// Gets the Validation Message
        /// </summary>
        public string CustomMessage
        {
            get
            {
                return (ErrorMessage);
            }
        }

        /// <summary>
        /// This will be true if the error message is business validation only
        /// </summary>
        public bool BusinessValidation
        {
            get
            {
                return (IsBusinessValidation);
            }
        }

        /// <summary>
        /// Provides formated complete exception details
        /// </summary>
        public string CustomErrorDetails
        {

            get
            {
                return (GetFormatedErrorDetails());
            }
        }

        public string ExceptionSource
        {
            get
            {
                if (ExceptionObj != null)
                {
                    return (ExceptionObj.Source.ToString());
                }
                else
                {
                    return (string.Empty);
                }
            }
        }
        /// <summary>
        /// Provides formated exception details
        /// </summary>
        /// <returns></returns>
        private string GetFormatedErrorDetails()
        {

            DateTime dtDateOccurred = System.DateTime.Now;

            StringBuilder excepdetails = new StringBuilder();
            excepdetails.AppendLine(dtDateOccurred.ToString("dd/MMM/yyyy") + " " + dtDateOccurred.ToLongTimeString() + " - " + "Exception occurred");
            excepdetails.AppendLine("Description: " + this.ErrorMessage);
            excepdetails.AppendLine("Exception Message: " + ExceptionObj.Message);

            if (ExceptionObj != null)
            {
                if (ExceptionObj.InnerException != null)
                {
                    excepdetails.AppendLine("Source: " + ExceptionObj.Source.ToString());
                    excepdetails.AppendLine("Original Exception Message: " + ExceptionObj.InnerException.ToString());
                }
                else if (ExceptionObj.Message != null)
                {
                    excepdetails.AppendLine("Original Exception Message: " + ExceptionObj.Message);
                }
            }
            excepdetails.Append(CallTrace.GetTrace);
            return (excepdetails.ToString());
        }

        /// <summary>
        /// This method will optionally log the Exception's message to a log file and the event log.
        /// </summary>
        public void WriteToLog(string LogFilePath, bool IsFileLogRequired, bool IsEventLogRequired)
        {

            string ErrorDetails = GetFormatedErrorDetails();
            string sDateFormat = "ddMMMyyyy";
                        
            //----------------------------------------------------------------
            //Log the exception to a file
            //----------------------------------------------------------------
            if (IsFileLogRequired)
            {
                //LogFilePath = AppDomain.CurrentDomain.BaseDirectory + LogFilePath;
                LogFilePath = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +"\\Visio Add-In for System Center\\" + LogFilePath;
                if (!System.IO.Directory.Exists(LogFilePath))
                {
                    System.IO.Directory.CreateDirectory(LogFilePath);
                }
                string LogPathAndFile =  LogFilePath + "\\" + System.DateTime.Now.ToString(sDateFormat) + "-VisAdException.log";
                TextWriter oTR = File.AppendText(LogPathAndFile);
                oTR.WriteLine(ErrorDetails);
                oTR.Close();
            }

            //----------------------------------------------------------------
            //Log the exception to the event log
            //----------------------------------------------------------------
            if (IsEventLogRequired)
            {
                EventLog.WriteEntry("Visio AddIn", ErrorDetails, System.Diagnostics.EventLogEntryType.Error);
            }
        }
        
        /// <summary>
        /// The tracks and add the method calls stack
        /// </summary>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        public void AddTrace(string className, string methodName)
        {
            CallTrace.Add(className, methodName);
        }
    }

  
}
